package com.mingmen.canting.gongyingshang;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.mingmen.canting.R;
import com.mingmen.canting.activity.DefaultListActivity;
import com.mingmen.canting.base.BaseBean;
import com.mingmen.canting.fragment.BaseFragment;
import com.mingmen.canting.http.callback.StringCallback;
import com.mingmen.canting.httpconfig.HttpResponse;
import com.mingmen.canting.model.EventBean;
import com.mingmen.canting.model.GongYingDetailLeft;
import com.mingmen.canting.util.GsonUtil;
import com.mingmen.canting.util.MyStringUtils;
import com.mingmen.canting.util.SharePreferenceUtil;
import com.mingmen.canting.zhenbanlibrary.CheckListener;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import okhttp3.Call;
import okhttp3.Response;

@SuppressLint("ValidFragment")
public class GongYingNewDetailFragment extends BaseFragment implements CheckListener, ShopDetailLeftAdapter.OnItemClickListener, View.OnClickListener {

    private RecyclerView rvSort;
    private ShopDetailLeftAdapter mSortAdapter;


    String getData;


    List<GongYingDetailLeft.CommodiousBean> childrenBeanList = new ArrayList<>();
    GongYingDetailLeft zhenBanLeftBean;
    Gson gson;


    LinearLayout recycle_view;
    LinearLayout layout_nodata;
    List<GongYingDetailLeft.SecondCategoriesBean.CommodiousBeanX> secondCategoriesBeanList = new ArrayList<>();
    GongYingMyRightAdapter gongYingMyRightAdapter;
    RecyclerView rv_sortRight;
    String token;
    List<String> commodityIds = new ArrayList<>();
    private int resurtResurant = 100;
    List<String> supplierIds = new ArrayList<>();
    String supplierId;

    @Override
    public View initView() {
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.fr_gongyingnewdetail, null);
        TextView card_title_tv = (TextView) v.findViewById(R.id.card_title_tv);

        recycle_view = v.findViewById(R.id.recycle_view);
        layout_nodata = v.findViewById(R.id.layout_nodata);
        EventBus.getDefault().register(this);
        card_title_tv.setText(getData);
        mContext = getActivity();
        gson = new Gson();
        rvSort = (RecyclerView) v.findViewById(R.id.rv_sort);
        rvSort.setLayoutManager(new LinearLayoutManager(getActivity()));
        token = (String) SharePreferenceUtil.get(getActivity(), "token", "");
        supplierId = (String) SharePreferenceUtil.get(getActivity(), "supplierId", "");
        Log.d("getDatagetData", getData);
        Log.d("onCreateView", "onCreateView");
        rv_sortRight = v.findViewById(R.id.rv_sortRight);
        zhenBanLeftBean = new GongYingDetailLeft();
        zhenBanLeftBean = gson.fromJson(getData, GongYingDetailLeft.class);

        mSortAdapter = new ShopDetailLeftAdapter(getActivity(), childrenBeanList);
        rvSort.setAdapter(mSortAdapter);
//        childrenBeanList = zhenBanLeftBean.getChildren();
        childrenBeanList.addAll(zhenBanLeftBean.getCommodious());
        Log.d("zhenBanLeftBean", childrenBeanList.size() + "");

        Log.d("childrenBeanList", childrenBeanList.size() + "");
        for (int i = 0; i < childrenBeanList.size(); i++) {
            if (childrenBeanList.get(i).getIsDefault().equals("1")) {
                setChecked(i, true);
            }
        }

        mSortAdapter.setDataList(childrenBeanList);
        mSortAdapter.setOnItemClickListener(this);
        rv_sortRight.setLayoutManager(new LinearLayoutManager(getActivity()));
        gongYingMyRightAdapter = new GongYingMyRightAdapter(getActivity(), secondCategoriesBeanList);
        rv_sortRight.setAdapter(gongYingMyRightAdapter);
        if (zhenBanLeftBean.getSecondCategories().size() == 0) {
            rv_sortRight.setVisibility(View.GONE);
            layout_nodata.setVisibility(View.VISIBLE);
        } else {
            rv_sortRight.setVisibility(View.VISIBLE);
            layout_nodata.setVisibility(View.GONE);

            for (int i = 0; i < zhenBanLeftBean.getSecondCategories().size(); i++) {
                secondCategoriesBeanList.addAll(zhenBanLeftBean.getSecondCategories().get(i).getCommodious());
            }

            gongYingMyRightAdapter.setDataList(secondCategoriesBeanList);
            gongYingMyRightAdapter.notifyDataSetChanged();
        }


        gongYingMyRightAdapter.setOnItemClickListener(new GongYingMyRightAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View view, int pos, List<GongYingDetailLeft.SecondCategoriesBean.CommodiousBeanX> myLiveList) {
                Intent intent;
                switch (view.getId()) {

                    case R.id.checkbox_moren:
//                0-无默认供应商  1-本单位默认  2-非本单位默认
                        if (myLiveList.get(pos).getIsDefault().equals("0")) {
                            setDefaultCommodity(token, supplierId, myLiveList.get(pos).getCommodityId(), myLiveList.get(pos).getIsDefault(), myLiveList, pos);
                        } else if (myLiveList.get(pos).getIsDefault().equals("1")) {
                            setDefaultCommodity(token, supplierId, myLiveList.get(pos).getCommodityId(), myLiveList.get(pos).getIsDefault(), myLiveList, pos);
                        } else {
                            myLiveList.get(pos).setIsDefault(myLiveList.get(pos).getIsDefault());
                            gongYingMyRightAdapter.notifyDataSetChanged();
                            intent = new Intent(getActivity(), DefaultListActivity.class);
                            intent.putExtra("categoryName", myLiveList.get(pos).getDefaultSupplier());
                            intent.putExtra("commodityName", myLiveList.get(pos).getCommodityName());
                            intent.putExtra("commodityId", myLiveList.get(pos).getCommodityId());
                            intent.putExtra("pos", pos + "");
                            intent.putExtra("isDefault", myLiveList.get(pos).getIsDefault());
                            startActivityForResult(intent, resurtResurant);
                        }


                        break;
                }
            }
        });

        return v;
    }

    @Override
    public void initData() {

    }


    @Override
    public void onResume() {
        super.onResume();
        Log.d("onResume", "onResume");


    }


    public static GongYingNewDetailFragment getInstance(String title) {
        GongYingNewDetailFragment sf = new GongYingNewDetailFragment();
        sf.getData = title;
        Log.d("DingDandkdkdkd", title);

        return sf;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    String myreceiveposition;

    private void setChecked(int position, boolean isLeft) {
        Log.d("p-------->", String.valueOf(position));

        mSortAdapter.setCheckedPosition(position, isLeft);
        myreceiveposition = String.valueOf(position);

    }


    @Override
    public void check(int position, boolean isScroll) {
//        setChecked(position, isScroll);
    }


    public void onEventMainThread(EventBean eventBean) {


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {


        }
    }

    @Override
    public void onItemClickListener(View view, int pos, List<GongYingDetailLeft.CommodiousBean> myLiveLists) {


    }

    String companyId;

    public void setDefaultCommodity(String token, String supplierId, String commodityId, String isDefault, List<GongYingDetailLeft.SecondCategoriesBean.CommodiousBeanX> myLiveList, int pos) {
        HttpResponse.setDefaultCommodity(token, supplierId, commodityId, new StringCallback(getActivity()) {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                Log.d("setDefaultCommodity", s);

                BaseBean baseBean = new BaseBean();
                baseBean = GsonUtil.GsonToBean(s, BaseBean.class);
                if (baseBean.getCode() == 0) {
                    if (isDefault.equals("0")) {
                        myLiveList.get(pos).setIsDefault("1");
                        setChecked(pos, true);
                    } else {
                        myLiveList.get(pos).setIsDefault("0");
                        setChecked(pos, false);
                    }
                    gongYingMyRightAdapter.notifyDataSetChanged();
                    mSortAdapter.notifyDataSetChanged();
                }
                Toast.makeText(getActivity(), baseBean.getMessage() + "", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            companyId = data.getStringExtra("companyId");
            String sendCommodity = MyStringUtils.returnStringBuffer(commodityIds);

            String commodityId = data.getStringExtra("commodityId");
            String pos = data.getStringExtra("commodityId");
            String isDefault = data.getStringExtra("isDefault");
            for (int i = 0; i < secondCategoriesBeanList.size(); i++) {
                secondCategoriesBeanList.get(i).setIsDefault("0");
                gongYingMyRightAdapter.notifyDataSetChanged();
            }

            setDefaultCommodity(token, supplierId,commodityId,isDefault, secondCategoriesBeanList, Integer.valueOf(pos));
            /* HttpResponse.setDefaultSupplier(token, supplierId, new StringCallback(getActivity()) {
                @Override
                public void onSuccess(String s, Call call, Response response) {
                    BaseBean baseBean = new BaseBean();
                    baseBean = GsonUtil.GsonToBean(s, BaseBean.class);
                    if (baseBean.getCode() == 0) {
                        for (int i = 0; i < secondCategoriesBeanList.size(); i++) {
                            secondCategoriesBeanList.get(i).setIsDefault("0");
                            gongYingMyRightAdapter.notifyDataSetChanged();
                            childrenBeanList.get(i).setIsDefault("0");
                            mSortAdapter.notifyDataSetChanged();
                        }
                    }
                    Toast.makeText(getActivity(), baseBean.getMessage() + "", Toast.LENGTH_SHORT).show();
                }
            });*/
        } catch (Exception e) {

        }


    }
}